if (!base.questions) base.questions = {};

base.questions = {
	init:function(){
		//containers
		this.itemsContainer = $(".itemsWrapper .itemList");
		this.itemForm = $(".itemsWrapper .frm.body");

		if($(".btn-add")[0]){
			this.addItemHandler();
		}
		this.updateItems();
		this.deleteItems();
		this.formItems = $('.frm.body');

		base.subscribe(this.itemForm, "frm.success", $.proxy(function(){
			this.loadItems();
			setTimeout(function(){
				$(".disable").trigger("click");
			}, 1000);
		}, this));

		$(".toggle label", this.itemForm).on("click", function(){
			$correct = $(this.itemsContainer).find("info .correct");
			if($("#is_multiple").val() == 0 && $(this.itemsContainer).find(".correct").size() > 0){
				base.showAlert("This is not a multiple answer type question.");
			}
		});
	},
	addItemHandler: function(){
		$btn = $(".btn-add")[0], $cancel = $(".body .cancel");
		$itemForm = $(".itemsWrapper .frm.body");
		
		$itemForm.hide();
		$($btn).on("click", $.proxy(function(e){
			e.preventDefault();
			if($(this.itemsContainer).find(".item").size() == 4){//limited to 4 items per question.
				base.showAlert("Each question is limited to 4 options only. In order to add a new option, please remove one.", true);
				$itemForm.slideUp("slow");
			}else{
				$itemForm.slideDown("slow");
				$(this.formItems).attr("data-type", "add");
			}
			$(this.itemForm).find("#ite_id").val("");
			$(this.formItems).data("frm").resetForm();
		}, this));
		$($cancel).on("click", $.proxy(function(e){
			e.preventDefault();
			$itemForm.slideUp("slow");
			$(this.formItems).data("frm").resetForm();
		}, this));
	},
	updateItems:function(){
		$btn = $(this.itemsContainer).find(".update");

		//update item
		$btn.on('click', $.proxy(function(e){

			$item = $(e.target).parent().parent();
			//set form
			$(this.itemForm).attr("data-type", "update");
			$(this.itemForm).find("#itemDescription").val($item.find(".description").data("text"));
			$(this.itemForm).find("#ite_id").val($(e.target).data("id"));
			$success = $item.find(".alert-success");

			$(this.itemForm).find(".toggle").find('label').removeClass("active");
			if($success[0]){
				$(this.itemForm).find(".correct input").prop("checked", true);
				$(this.itemForm).find(".incorrect input").prop("checked", false);
				$(this.itemForm).find(".toggle .correct").addClass("active");
				
			}else{
				$(this.itemForm).find(".correct input").prop("checked", false);
				$(this.itemForm).find(".incorrect input").prop("checked", true);
				$(this.itemForm).find(".toggle .incorrect").addClass("active");
			}
			$(this.itemForm).slideDown("slow");
		}, this));
	},
	deleteItems:function(){
		if($('.item-delete')[0]){
		  var deleteId = 0,
		      $confirm = $('#confirmDialog'), me = this;

		   /*delete flow*/
		  $('.item-delete').off().on('click', function(e){
		      e.preventDefault();
		      //show confirm dialog
		      $confirm.modal("show");
		      deleteId = $(e.target).data("id");
		  });

		  $('.btn-ok.confirm-delete').off().on('click', function(e){
		      e.preventDefault();
		      $confirm.modal("hide");

		      $.ajax({
		          url: $($(".delete-link")[0]).val(),
		          dataType: 'json',
		          type: 'POST',
		          contentType: 'application/json',
		          data: '{"ite_id":"'+deleteId+'", "que_id":"'+$("#questionId").val()+'"}',
		          complete: function(resp) {
		              if (resp.statusText == "OK") {
		                  base.showSuccess("Record was deleted successfully.");
		                  //remove and load all items again..
		                  $(".item-"+deleteId).remove();
		                  me.loadItems();
		              } else {
		                  base.showError("There was an error.");
		              }

		          }
		      });

		  });
		  /*end delete flow*/
		}
	},
	loadItems: function(){
		$(this.itemsContainer).html($('<span class="loader"></span>'));
		// submit via ajax
		var itemsContainer = this.itemsContainer;
		$.ajax({
		    url: $(this.itemsContainer).data("load"),
		    type: "POST",
		    data: '{"id":"'+$("#questionId").val()+'"}',
		    type: "POST",
		    dataType: "json",
		    contentType: "application/json",
		    cache: false,
		    complete:function(data){
		    	if(data.statusText == "OK"){
		            $(itemsContainer).html(data.responseText);
		        }
		    }
		});
	}
};